<template>
  <div>
    <!--查询区域-->
    <!-- <div class="jeecg-basic-table-form-container">
      <a-form ref="formRef" @keyup.enter.native="searchQuery" :model="queryParam" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    &lt;!&ndash;引用表格&ndash;&gt;
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      &lt;!&ndash;插槽:table标题&ndash;&gt;
      <template #tableTitle>
        <a-button type="primary" @click="handleAdd" preIcon="ant-design:plus-outlined"> 新增</a-button>
        <a-button  type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <j-upload-button  type="primary" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      &lt;!&ndash;操作栏&ndash;&gt;
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
      &lt;!&ndash;字段回显插槽&ndash;&gt;
      <template #htmlSlot="{text}">
        <div v-html="text"></div>
      </template>
      &lt;!&ndash;省市区字段回显插槽&ndash;&gt;
      <template #pcaSlot="{text}">
        {{ getAreaTextByCode(text) }}
      </template>
      <template #fileSlot="{text}">
        <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    &lt;!&ndash; 表单区域 &ndash;&gt;
    <WelfareConfigModal ref="registerModal" @success="handleSuccess"></WelfareConfigModal>-->

    <a-form ref="formRef" :model="queryParam" style="margin-top: 46px" class="my-form">
      <a-col>
        <a-form-item label="网站连续签到赠送bonus" class="form-line">
          <div v-for="(item, index) in formArr.arr0" class="form-line-div form-line-div-short">
            <sapn>{{ item }}Day</sapn>
            <a-input-number :min="0" placeholder="请输入" v-model:value="queryParam.arr0['day' + item]" />
          </div>
        </a-form-item>
      </a-col>
      <a-col>
        <a-form-item label="网站阅读时长赠送bonus" class="form-line">
          <div v-for="(item, index) in formArr.arr1" class="form-line-div">
            <sapn>{{ item * 30 }}min</sapn>
            <a-input-number :min="0" placeholder="请输入" v-model:value="queryParam.arr1['min' + item]" />
          </div>
        </a-form-item>
      </a-col>
      <a-col>
        <a-form-item label="网站每天评论赠送bonus" name="comment" class="form-line">
          <a-input-number :min="0" placeholder="请输入" v-model:value="queryParam.comment" />
        </a-form-item>
      </a-col>
      <a-col>
        <a-form-item label="每日分享赠送bonus" name="share" class="form-line">
          <a-input-number :min="0" placeholder="请输入" v-model:value="queryParam.share" />
        </a-form-item>
      </a-col>
      <a-col>
        <a-form-item label="加入telegram群组赠送bonus" name="join_tg" class="form-line">
          <a-input-number :min="0" placeholder="请输入" v-model:value="queryParam.join_tg" />
        </a-form-item>
      </a-col>
      <a-col>
        <a-form-item label="APP第一次登录赠送bonus" name="join_app" class="form-line">
          <a-input-number :min="0" placeholder="请输入" v-model:value="queryParam.join_app" />
        </a-form-item>
      </a-col>

      <a-col class="one-line-col">
        <a-form-item label="APP每天任务广告上限" name="ad_task_limit" class="form-line">
          <a-input-number :min="0" placeholder="请输入" v-model:value="queryParam.ad_task_limit" />
        </a-form-item>
        <a-form-item label="任务广告赠送bonus" name="ad" class="form-line">
          <a-input-number :min="0" placeholder="请输入" v-model:value="queryParam.ad" />
        </a-form-item>
      </a-col>
      <a-col>
        <a-form-item label="APP每天广告解锁上线" name="ad_unlock_limit" class="form-line">
          <a-input-number :min="0" placeholder="请输入" v-model:value="queryParam.ad_unlock_limit" />
        </a-form-item>
      </a-col>

      <a-col>
        <a-form-item label=" " name="" :colon="false" class="form-line">
          <a-button type="primary" @click="clickSubmit">保存</a-button>
        </a-form-item>
      </a-col>
    </a-form>
  </div>
</template>

<script lang="ts" name="novel-welfareConfig" setup>
  import { ref, reactive } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns } from './WelfareConfig.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl } from './WelfareConfig.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import WelfareConfigModal from './components/WelfareConfigModal.vue';
  import { defHttp } from '/@/utils/http/axios';
  import { message } from 'ant-design-vue';
  const formRef = ref();
  const queryParam = reactive<any>({
    arr0: {
      day1: '',
      day2: '',
      day3: '',
      day4: '',
      day5: '',
      day6: '',
      day7: '',
    },
    arr1: {
      min1: '',
      min2: '',
      min3: '',
      min4: '',
    },
    comment: '',
    ad: '',
    data: [],
    share: '',
    join_tg: '',
    join_app: '',
    ad_task_limit:'',
    ad_unlock_limit:''
  });
  const formArr = reactive<any>({
    arr0: [1, 2, 3, 4, 5, 6, 7],
    arr1: [1, 2, 3, 4],
  });

  async function init() {
    let data = await list({name:''})
    data.forEach((item,index)=>{
      if (item.name==='check_in'){
        queryParam.arr0 = JSON.parse(data[index].value);
      }else if (item.name==='read'){
        queryParam.arr1 = JSON.parse(data[index].value);
      }
      else {
        queryParam[item.name]=data[index].value
      }
    })

    data=data.filter(item=>item.name!=='sub_price')
    queryParam.data = data;

  }

  init();

  const toggleSearchStatus = ref<boolean>(false);
  const registerModal = ref();
  //注册table数据
  const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
    tableProps: {
      title: 'welfare_config',
      api: list,
      columns,
      canResize: false,
      useSearchForm: false,
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        return Object.assign(params, queryParam);
      },
    },
    exportConfig: {
      name: 'welfare_config',
      url: getExportUrl,
      params: queryParam,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });
  const [registerTable, { reload, collapseAll, updateTableDataRecord, findTableDataRecord, getDataSource }, { rowSelection, selectedRowKeys }] =
    tableContext;
  const labelCol = reactive({
    xs: { span: 7 },
    sm: { span: 7 },
  });
  const wrapperCol = reactive({
    xs: { span: 24 },
    sm: { span: 24 },
  });

  /**
   * 新增事件
   */
  function handleAdd() {
    registerModal.value.disableSubmit = false;
    registerModal.value.add();
  }

  /**
   * 编辑事件
   */
  function handleEdit(record: Recordable) {
    registerModal.value.disableSubmit = false;
    registerModal.value.edit(record);
  }

  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    registerModal.value.disableSubmit = true;
    registerModal.value.edit(record);
  }

  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }

  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }

  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }

  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
      },
    ];
  }

  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
        },
      },
    ];
  }

  /**
   * 查询
   */
  function searchQuery() {
    reload();
  }

  /**
   * 重置
   */
  function searchReset() {
    formRef.value.resetFields();
    selectedRowKeys.value = [];
    //刷新数据
    reload();
  }

  async function clickSubmit() {
    let url = queryParam.id === '' ? '/novel/welfareConfig/add' : '/novel/welfareConfig/edit';
    queryParam.data[0].value = JSON.stringify(queryParam.arr0);
    queryParam.data[1].value = JSON.stringify(queryParam.arr1);
    let arr=['comment','ad','share','join_tg','join_app','ad_task_limit','ad_unlock_limit']
    arr.forEach((item,index)=>{
      let i=index+2
      queryParam.data[i].value = queryParam[item];
    })
    const data = await defHttp.post({ url, params: queryParam.data });
  }
</script>

<style lang="less" scoped>
  .jeecg-basic-table-form-container {
    .table-page-search-submitButtons {
      display: block;
      margin-bottom: 24px;
      white-space: nowrap;
    }
    .query-group-cust {
      width: calc(50% - 15px);
      min-width: 100px !important;
    }
    .query-group-split-cust {
      width: 30px;
      display: inline-block;
      text-align: center;
    }
  }
  .my-form {
    /deep/.ant-form-item-control {
      min-width: unset;
    }
    .form-line {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      flex-wrap: nowrap;
      /deep/.ant-input-number {
        max-width: 180px;
        min-width: 100px;
        margin-left: 6px;
      }
      .form-line-div {
        display: flex;
        align-items: center;
        margin-right: 10px;
      }
      .form-line-div-short {
        /deep/.ant-input-number {
          max-width: 120px;
          min-width: 100px;
          margin-left: 6px;
        }
      }
      /deep/.ant-form-item-label {
        width: 210px;
      }

      /deep/.ant-form-item-control-input-content {
        display: flex;
      }
    }
  }

  .one-line-col{
    display: flex;
  }
</style>
